package com.adinnet.admin.information.controller;


import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.information.model.Information;
import com.adinnet.admin.information.model.vo.InformationReq;
import com.adinnet.admin.information.service.InformationService;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 资讯管理 前端控制器
 * </p>
 *
 * @author chenjia
 * @since 2020-12-17
 */
@Controller
@RequestMapping("admin/information")
public class InformationController {

    @Autowired
    private InformationService informationService;

    @Autowired
    private CommonService commonService;

    @RequestMapping()
    public String information() {
        return "information/information.html";
    }

    /**
     * 查询所有资讯
     **/
    @ResponseBody
    @RequestMapping("list")
    public PageResult<Information> list(InformationReq req) {
        return informationService.List(req);

    }


    /**
     *  新增资讯
     * @param information
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("add")
    public JsonResult add(Information information, HttpServletRequest request) {
        if (informationService.save(information)) {
            commonService.saveOperation(request,"资讯管理-新增资讯,资讯id为："+information.getId());
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }
    }


    /**
     *
     * @param information
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
    public JsonResult update(Information information,HttpServletRequest request) {
        if (informationService.updateById(information)) {
            commonService.saveOperation(request,"资讯管理-编辑资讯,资讯id为："+information.getId());
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }
    }


    @ResponseBody
    @RequestMapping("updateState")
    public JsonResult updateState(Integer informationId, Integer state, HttpServletRequest request) {
        if (informationId == null) {
            return JsonResult.error("参数informationId不能为空");
        }
        if (state == null || (state != 0 && state != 1)) {
            return JsonResult.error("状态值不正确");
        }
        Information Information = new Information();
        Information.setId(informationId);
        Information.setStatus(state);
        if (informationService.updateById(Information)) {
            commonService.saveOperation(request,"资讯管理-修改资讯状态");
            return JsonResult.ok();
        } else {
            return JsonResult.error();
        }
    }


    @ResponseBody
    @RequestMapping("delete")
    public JsonResult delete(Integer informationId) {
        if (informationService.removeById(informationId)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @RequestMapping("deleteIds")
    @ResponseBody
    public JsonResult deleteIds(String informationIds, HttpServletRequest request) {
        commonService.saveOperation(request,"资讯管理-删除资讯：删除的资讯id为："+informationIds);
        return informationService.deleteIds(informationIds);
    }

}
